package com.atqinke.designpattern.creationtype.singlecase;

/**
 * @BelongsProject: BasicConnection
 * @BelongsPackage: com.atqinke.DesignPattern.creation.SingleCase
 * @Author: NieHk
 * @CreateTime: 2022-08-16  09:39
 * @Description: 懒汉式实现单例模式①(线程不安全)
 * @Version: 1.0
 */
public class Sluggard {
    //声明对象
    private static Sluggard sluggard;
    //私有构造器
    private Sluggard() {
    }
    //提供获取方式
    public static Sluggard getSluggard() {
        if (sluggard == null) {
            sluggard = new Sluggard();
        }
        return sluggard;
    }
}

/**
* @description: 懒汉式实现单例模式2(线程安全)
* @author: NieHK
* @date: 2022/8/16 9:43
* @param
* @return:
*/
class SluggardTwo{
    //声明对象 加volatile防止重排序
    private volatile static SluggardTwo sluggardTwo;
    //私有构造器
    private SluggardTwo() {
    }
    //提供获取方式
    public static SluggardTwo getSluggardTwo() {
        if (sluggardTwo == null) {
            synchronized (SluggardTwo.class){
                if (sluggardTwo == null){
                    sluggardTwo = new SluggardTwo();
                }
            }
        }
        return sluggardTwo;
    }
}